package titt.util;

import java.util.HashMap;
import java.util.Map;

import titt.config.ValueConfig;

public class Bytes {
		
	public static byte[] asBytes(String s) {
		return s.getBytes(ValueConfig.get().getCharset());
	}
	public static byte[][] asBytes(String[] s) {
		byte[][] bs = new byte[s.length][];
		for(int i = 0; i < s.length; i++) {
			bs[i] = Bytes.asBytes(s[i]);
		}
		return bs;
	}
	public static <T> Map<T, byte[]> asBytes(Map<T, String> m) {
		Map<T, byte[]> ret = new HashMap<T, byte[]>();
		for(T k : m.keySet())
			ret.put(k, asBytes(m.get(k)));
		return ret;
	}
	public static String asString(byte[] bs) {
		return new String(bs, ValueConfig.get().getCharset());
	}
	public static String[] asString(byte[][] bs) {
		String[] s = new String[bs.length];
		for(int i = 0; i < bs.length; i++)
			s[i] = Bytes.asString(bs[i]);
		return s;
	}
	public static <T> Map<T, String> asString(Map<T, byte[]> m) {
		Map<T, String> ret = new HashMap<T, String>();
		for(T k : m.keySet())
			ret.put(k, asString(m.get(k)));
		return ret;
	}

	public static byte[] copy(byte[] b, int fromIndex, int length) {
		byte[] copy = new byte[length];
		System.arraycopy(b, fromIndex, copy, 0, length);
		return copy;
	}
}
